/**
 * @MovieDataUtils.as
 *
 * @author sodaChen mail:sujun10@qq.com
 * @version 1.0
 * <br>Program Name:ASFrame
 * <br>Date:2016-9-29
 */
package com.asframe.utils
{
	import com.asframe.display.data.FrameData;
	import com.asframe.display.data.MovieData;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;


	/**
	 * 解析并生成MovieData数据
	 * @author sodaChen
	 * #Date:2016-9-29
	 */
	public class MovieDataUtils
	{
		public static function parseBitmapData(bitmapData:BitmapData,row:int,col:int):Vector.<MovieData>
		{
			//先切割出大小一样的BitmapDatas
			var bitmapDatas:Vector.<Vector.<BitmapData>> = BitmapDataUtils.carveup(bitmapData,row,col);
			//生成新的MovieData数组
			var movieDatas:Vector.<MovieData> = new Vector.<MovieData>();
			var movieData:MovieData = null;
			var frameDatas:Vector.<FrameData> = null;
			var frameData:FrameData = null;
			var bitmap:Bitmap = new Bitmap();
			var simpleBitmapData:BitmapData = null;
			var rect:Rectangle = null;
			var newBitmapDatas:Vector.<BitmapData> = null;
			var matrix:Matrix = null;
			for(var i:int = 0; i < row; i++)
			{
				//纵
				frameDatas = new Vector.<FrameData>();
				newBitmapDatas = new Vector.<BitmapData>();
				movieData = new MovieData(newBitmapDatas,frameDatas);
				movieDatas.push(movieData);
				for(var j:int = 0; j < col; j++)
				{
					bitmapData = bitmapDatas[i][j];
					//获取位图的最小范围
					rect = bitmapData.getColorBoundsRect(0xFFFFFFFf, 0xFFFFFF, false);
					//重新生成一个新BitmapData
					rect.width = Math.ceil(rect.width);
					rect.width = rect.width == 0 ? 1 : rect.width;
					rect.height = Math.ceil(rect.height);
					rect.height = rect.height == 0 ? 1 : rect.height;
					rect.left = Math.floor(rect.left);
					rect.top = Math.floor(rect.top);
					matrix = new Matrix();
					matrix.translate(-rect.x,-rect.y);
					simpleBitmapData = new BitmapData(rect.width, rect.height, true, 0);
					simpleBitmapData.draw(bitmapData, matrix);
					newBitmapDatas.push(simpleBitmapData);
					frameData = new FrameData(simpleBitmapData,rect);
					frameDatas.push(frameData);
				}
			}
			return movieDatas;
		}
	}
}